package com.das.mechanic_base.adapter.intensivetain;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.custrecord.AloneDetectionBean;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.widget.X3TainNewTimeView;

import java.util.ArrayList;
import java.util.List;

public class X3AloneImageWorkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<AloneDetectionBean> mList;

    public X3AloneImageWorkAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeAlone(List<AloneDetectionBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_record_item, parent, false));
        }
        return new AloneImageWorkHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_alone_image_work_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 1) {
            AloneImageWorkHolder workHolder = (AloneImageWorkHolder) holder;

            workHolder.tv_title.setText(mList.get(position).serviceBaseName + "");

            workHolder.tv_store.setText(X3StringUtils.isListEmpty(mList.get(position).staffBaseNameList) ? "" : mList.get(position).staffBaseNameList.get(0));

            workHolder.tv_time.setText(X3DateUtils.LongDateWithTime(mList.get(position).completeTime));

            double tatalPercent = mList.get(position).tatalPercent * 100;

            workHolder.tv_view.changeAngle((int) tatalPercent);

            workHolder.tv_grade.setText(X3StringUtils.formatDouble(tatalPercent));

            holder.itemView.setOnClickListener(v -> {
                if (iOnClickAlone != null) {
                    iOnClickAlone.iOnClickAloneDetail(mList.get(position),mList.get(position).serviceBaseName);
                }
            });
        }else {
            EmptyHolder emptyHolder = (EmptyHolder) holder;
            emptyHolder.tv_empty.setText(mContext.getString(R.string.x3_home_no_select));
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (X3StringUtils.isListEmpty(mList)) {
            return 0;
        }
        return 1;
    }

    IOnClickAlone iOnClickAlone;

    public void setiOnClickAlone(IOnClickAlone iOnClickAlone) {
        this.iOnClickAlone = iOnClickAlone;
    }

    public interface IOnClickAlone {

        void iOnClickAloneDetail(AloneDetectionBean detectionBean,String title);
    }

    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? 1 : mList.size();
    }

    class AloneImageWorkHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_store;
        TextView tv_time;
        X3TainNewTimeView tv_view;
        TextView tv_grade;
        public AloneImageWorkHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_store = itemView.findViewById(R.id.tv_store);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_view = itemView.findViewById(R.id.tv_view);
            tv_grade = itemView.findViewById(R.id.tv_grade);
        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;

        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
        }
    }

}
